<template>
  <readcard :id="this.$route.query.id" :chapterId="this.$route.query.chapter_id" />
</template>

<script>
import readcard from "@/components/ReadCard";
const moment = require("moment");
moment.locale("zh-cn");
export default {
  name: "read",
  components: {
    readcard
  },
  data() {
    return {};
  },
  created() {
    if (!window.localStorage.getItem("recently_read")) {
      let id = this.$route.query.id;
      let chapterId = this.$route.query.chapter_id;
      let current = {
        id,
        chapterId,
        lastReadTime: moment().format("YYYY-MM-D h:mm"),
      };
      let recently_read = JSON.stringify([current]);
      window.localStorage.setItem("recently_read", recently_read);
    } else {
      let recently_read = JSON.parse(
        window.localStorage.getItem("recently_read")
      );
      if (
        recently_read.filter(item => item.id == this.$route.query.id).length >
        0
      ) {
        // 有该文章的阅读记录，修改
        recently_read.forEach(item => {
          if (item.id == this.$route.query.id) {
            item.chapterId = this.$route.query.chapter_id;
            item.lastReadTime = moment().format("YYYY-MM-D h:mm");
          }
        });
        window.localStorage.setItem(
          "recently_read",
          JSON.stringify(recently_read)
        );
      } else {
        // 没有该文章的阅读记录，添加
        recently_read.push({
          id: this.$route.query.id,
          chapterId: this.$route.query.chapter_id,
          lastReadTime: moment().format("YYYY-MM-D h:mm"),
        });
        window.localStorage.setItem(
          "recently_read",
          JSON.stringify(recently_read)
        );
      }
    }
  },
  methods: {}
};
</script>

<style lang="scss" scoped>
</style>